<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- <el-checkbox label="110">管理员</el-checkbox>
      <el-checkbox label="113">开发者</el-checkbox>
      <el-checkbox label="115">人事</el-checkbox> -->
      <el-checkbox
        v-for="item in roleList"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>
    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getUserDetail } from '@/api/user'
import { getRoles } from '@/api/settings'
import { assignRoles } from '@/api/employee'
export default {
  props: {
    // 用户的id 用来查询当前用户的角色信息
    employeId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  // created() {
  //   this.loadRoles()
  // },
  methods: {
    // 获取所有角色
    async loadRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 50 })
        console.log(res)
        // 保存所有的角色
        this.roleList = res.data.rows
        const info = await getUserDetail(this.employeId)
        console.log(info)
        // 用户角色回填
        this.roleIds = info.data.roleIds
      } catch (err) {
        console.log(err)
      }
    },
    // 取消
    closeDialog() {
      this.$emit('close')
    },
    // 确定
    async  hSubmit() {
      try {
        const res = await assignRoles({ id: this.employeId, roleIds: this.roleIds })
        console.log(res)
        this.$emit('close')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>
